
// pages/user/user.js
const urls = require('../../utils/url.js').urls;
const http = require('../../utils/url.js').http
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dwArray:[],
    dw:null,
    dwid: null,
    dzArray: [],
    dz: null,
    bmArray: [],
    bm: null,
    bmid:null,
    name: '',
    zj: '',
    sj: '',
    fj: '',
  },

  //选择单位
  binddwChange : function(event){
    var that = this;
    var index = event.detail.value;
    var dw = that.data.dwArray[index].address;
    var dwid = that.data.dwArray[index].id;
    that.setData({
      dw: dw,
      dwid: dwid
    })

  },
  //选择地址
  binddzChange: function (event) {
    var that = this;
    var index = event.detail.value;
    console.log('???'+event.detail.value)

    that.setData({
      dz: event.detail.value
    })

  },
  //选择部门
  bindbmChange: function (event) {
    var that = this;
    var index = event.detail.value;
    var bm = that.data.bmArray[index].name;
    var bmid = that.data.bmArray[index].id;
    that.setData({
      bm: bm,
      bmid: bmid
    })

  },

  /**
   * 提交
   */
  fromSubmit: function (event) {
    var dwid = this.data.dwid;
    var dz = this.data.dz;
    var bmid = this.data.bmid;
    var name = event.detail.value.name;
    var zj = event.detail.value.zj;
    var sj = event.detail.value.sj;
    var fj = event.detail.value.fj;
    if (dwid == null) {
      wx.showToast({
        title: '请选择单位',
      })
    } else if (dz == null) {
      wx.showToast({
        title: '请选择地址',
      })
    } 
    else if (bmid == null) {
      wx.showToast({
        title: '请选择部门',
      })
    } 
    else if (name == "" || name == undefined) {
      wx.showToast({
        title: '请输入姓名',
      })
    } 
    else if (zj == "" || zj == undefined) {
      wx.showToast({
        title: '请输入座机号',
      })
    }
    else if (sj == "" || sj == undefined) {
      wx.showToast({
        title: '请输入手机号',
      })
    }
    else if (fj == "" || fj == undefined) {
      wx.showToast({
        title: '请输入房间号',
      })
    }else{
      var param = {
        openid: app.globalData.openid    ,  
        company_id :dwid,                                               //单位id
        department_id :bmid,                                                 //部门id
        dompany_address :dz,                                              //地址
       realname   :name,                                           //姓名
       phone    :sj,                                            //手机号
       zjh    :zj,                                          //座机号
       room   :fj
      }
      wx.showNavigationBarLoading();
      http(urls.userAttestation,param,function(res){
        console.log(res)
        // 隐藏加载中
        wx.hideNavigationBarLoading();
        wx.reLaunch({
          url: '../user/home/home',
        })
      })
    }

  },
  getUserLocation: function () {

    //地址选择
    let that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res)

        that.setData({
          dz: res.address+res.name
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    //1. 显示加载中
    wx.showNavigationBarLoading();
    //发送请求
    http(urls.getUsercom, null, function (res) {

      that.setData({
        dwArray: res.data
      })
      console.log(that.data.dwArray)
      // 隐藏加载中
      wx.hideNavigationBarLoading();

    });
    // //发送请求
    // http(urls.getReg, null, function (res) {

    //   console.log(res.data)
    //   that.setData({
    //     dzArray: res.data
    //   })
    //   console.log(that.data.dwArray)
    //   // 隐藏加载中
    //   wx.hideNavigationBarLoading();

    // });
    //部门
    http(urls.getUserdep, null, function (res) {

      console.log(res.data)
      that.setData({
        bmArray: res.data
      })
      console.log(that.data.dwArray)
      // 隐藏加载中
      wx.hideNavigationBarLoading();

    });
    
    // 获取权限


    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              wx.startRecord()
            }
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})